<?php

	namespace app\home\controller;

	use app\home\validate\CartValidate;
	use think\Controller;
	use think\Request;
	use app\home\model\Cart as cartModel;
	use app\home\model\Goods as goodsModel;

	class Cart extends Base
	{

		public function add () {

			$cartValidate = new CartValidate();
			$validateRes = $cartValidate->goSceneCheck('add');
			if ($validateRes) {
				return error($validateRes);
			}

			$goods_id = (int) $this->request->post('goods_id');
			$total = (int) $this->request->post('total');
			$openid = $this->member->openid;

			$cartModel = new cartModel();
			$item = $cartModel->where(['goods_id' => $goods_id, 'openid' => $openid, 'deletetime' => 0])->find();

			//获取商品价格
			$goodsModel = new goodsModel();
			$goodsItem = $goodsModel->field('id,name,price,stock')->where(['id' => $goods_id])->find();

			if ($item) {//该商品已存在购物车中,修改数量即可
				$item->total += $total;
				$row = $item->save();

				if ($row > 0) {
					return success(['cart_id' => $item->id]);
				}

				return error('添加购物车失败');
			} else {

				if (empty($goodsItem)) {
					return error('该商品已删除');
				}

				if ($goodsItem->stock < $total) {
					return error('该商品最多可购买' . $goodsItem->stock);
				}

				$cartModel->openid = $openid;
				$cartModel->goods_id = $goods_id;
				$cartModel->total = $total;
				$cartModel->price = $goodsItem->price;
				$cartModel->createtime = time();

				$cartModel->save();
				$cart_id = $cartModel->id;

				if ($cart_id) {
					return success(['cart_id' => $cart_id]);
				}

				return error('添加购物车失败');
			}


		}

		public function getCartNum () {

			$cartModel = new cartModel();

			$cart_num = $cartModel->where(['openid' => $this->member->openid, 'deletetime' => 0])->sum('total');

			return success(['cart_num' => $cart_num]);
		}

		public function getCartGoodsList () {

			$cartModel = new cartModel();

			$list = $cartModel->with(['goods'])->where('deletetime', 0)->select();

			$counts = $total_price = 0;

			if (!empty($list)) {
				$total_price = $counts = 0;
				foreach ($list as $value) {
					if ($value->seleted == 1) {
						$total_price += $value->price * $value->total;
						$counts += $value->total;
					}
				}
			}


			return success(['counts' => $counts, 'total_price' => $total_price, 'list' => $list]);
		}

		/**
		 * 选择购物车
		 *
		 * @return mixed
		 *
		 * @param
		 *
		 * @throws
		 *
		 */
		public function selected () {
			$cartValidate = new CartValidate();

			$validateRes = $cartValidate->goSceneCheck('selected');

			if ($validateRes) {
				return error($validateRes);
			}

			$cart_id = (int) $this->request->post('cart_id');
			$seleted = (int) $this->request->post('seleted');

			$cartModel = new cartModel();
			$item = $cartModel->where('id', $cart_id)->find();
			$item->seleted = $seleted;
			$item->save();

			return success();
		}

		/**
		 * 全选操作
		 *
		 * @return mixed
		 *
		 * @param
		 *
		 * @throws
		 *
		 */
		public function allSelected () {
			$all = (int) $this->request->post('all');
			$openid = $this->member->openid;
			$cartModel = new cartModel();
			$cartModel->where(['openid' => $openid, 'deletetime' => 0])->update(['seleted' => $all]);

			return success();
		}

		/**
		 * 检测全选
		 *
		 * @return mixed
		 *
		 * @param
		 *
		 * @throws
		 *
		 */
		public function checkAllSeleted () {

			$openid = $this->member->openid;

			$cartModel = new cartModel();

			$cart_count = $cartModel->where(['openid' => $openid, 'deletetime' => 0])->count('id');

			$selected_crat_coutn = $cartModel->where(['openid' => $openid, 'deletetime' => 0, 'seleted' => 1])->count('id');

			if ($cart_count == $selected_crat_coutn && $cart_count !=0) {
				return success();
			}

			return error();
		}

		/**
		 * 修改购物车数量
		 *
		 * @return mixed
		 *
		 * @param  cart_id  购物车id
		 * @param  type     修改类型  add:增加 reduce:减少
		 *
		 * @throws
		 *
		 */
		public function updateCartStock () {

			$cartValidate = new CartValidate();
			$validateRes = $cartValidate->goSceneCheck('updateCartStock');
			if ($validateRes) {
				return error($validateRes);
			}

			$cart_id = $this->request->post('cart_id');
			$type = $this->request->post('type');

			$cartModel = new cartModel();
			$cartItem = $cartModel->with('goods')->where('id', $cart_id)->find();

			$msg = '';
			if ($type == 'add') {
				$total = $cartItem->total + 1;
				if ($total > $cartItem->goods->stock) {
					$total = $cartItem->goods->stock;
					$msg = '最多购买' . $cartItem->goods->stock . '件';
				}

			} else if ($type == 'reduce') {
				$total = $cartItem->total - 1;

				if ($total < 1) {
					$total = 1;
					$msg = '至少购买1件';
				}

			}

			$cartItem->total = $total;
			$cartItem->save();

			return success($msg);

		}

		/**
		 * 删除购物车
		 * @return mixed
		 * @param  cart_id
		 * @throws
		 *
		 */
		public function delCart () {
			$cartValidate = new CartValidate();
			$validateRes = $cartValidate->goSceneCheck('delCart');
			if ($validateRes) {
				return error($validateRes);
			}

			$cart_id = $this->request->post('cart_id');

			$cartModel = new cartModel();
			$cartItem = $cartModel->field('id,deletetime')->where('id', $cart_id)->find();

			if($cartItem){
				$cartItem->deletetime = time();
				$cartItem->save();
			}

			return success();
		}

		/**
		 * 检测购物车中默认选中的商品
		 * @return mixed
		 * @param
		 * @throws
		 *
		 */
		public function checkDefaultSelected(){

			$openid = $this->member->openid;
			$cartModel = new cartModel();
			$item = $cartModel->where(['seleted'=>1,'deletetime'=>0,'openid'=>$openid])->select();

			if (!empty($item)){
				return success();
			}

			return error('至少选择一个');
		}

		public function getGoodsListByCart(){

			$openid = $this->member->openid;

			$page = max(1,(int)$this->request->post('page'));
			$pagesize = 6;
			$cartModel = new cartModel();
			$list = $cartModel->with('goods')->where(['seleted'=>1,'deletetime'=>0,'openid'=>$openid])->limit( ( $page - 1) * $pagesize,$pagesize)->select();

			$total_price = 0;
			if($list){
				//计算总金额
				foreach ($list as $value){
					if($value->seleted == 1){
						$total_price += $value->price * $value->total;
					}
				}

				return success(['list'=>$list,'total_price'=>$total_price]);
			}

			return error('暂无数据');
		}

	}
